package com.wei.czz.framework.blog.service.impl;

import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.utils.SecurityUtils;
import com.wei.czz.framework.blog.dao.BlogAuditAssignDao;
import com.wei.czz.framework.blog.entity.BlogAuditAssignEntity;
import com.wei.czz.framework.blog.service.BlogAuditAssignService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2024-11-29 20:25:26
 * className: BlogAuditAssignServiceImpl
 * version: 1.0
 * description:
 */
@Service("blogAuditAssignService")
public class BlogAuditAssignServiceImpl extends ServiceImpl<BlogAuditAssignDao, BlogAuditAssignEntity>
        implements BlogAuditAssignService {

    private static final Logger log = LoggerFactory.getLogger(BlogAuditAssignServiceImpl.class);

    @Override
    public void saveOrEdit(BlogAuditAssignEntity blogAuditAssign) {
        // 获取操作用户
        String username = SecurityUtils.getOrDefaultUsername(StringUtils.EMPTY);
        Long userId = SecurityUtils.getOrDefaultUserId(CommonEnum.ZERO.getLongValue());
        Date date = new Date();

        if (Objects.isNull(blogAuditAssign.getId())) {
            blogAuditAssign.setDeleteStatus(CommonEnum.ZERO.getValue())
                    .setUpdateTime(date)
                    .setUpdateUser(StringUtils.EMPTY)
                    .setUpdateUserId(CommonEnum.ZERO.getLongValue())
                    .setCreateTime(date)
                    .setCreateUser(username)
                    .setCreateUserId(userId);

            int count = baseMapper.insert(blogAuditAssign);
            log.info("保存博客审核分配记录完成。count={}", count);
        } else {
            blogAuditAssign.setUpdateTime(date)
                    .setUpdateUser(username)
                    .setUpdateUserId(userId);

            int count = baseMapper.updateById(blogAuditAssign);
            log.info("修改博客审核分配记录完成。count={}", count);
        }
    }

    @Transactional
    @Override
    public void batchSave(List<BlogAuditAssignEntity> blogAuditAssignList) {
        boolean bool = this.saveBatch(blogAuditAssignList);
        log.info("批量保存博客审核分配记录完成。bool={}", bool);
    }
}